#!/bin/zsh

echo "--- Starting Schema Download ---"

# --- Configuration ---
BASE_URL="https://schema.cip4.org/"
SCHEMA_DIR="public/schemas"
TMP_DIR="/tmp"

# --- Argument Parsing ---
FORMAT="zip" # Default format
if [[ "$1" == "--format=tar" ]] || [[ "$1" == "tar" ]]; then
  FORMAT="tar"
echo "Selected format: tar.gz"
elif [[ "$1" == "--format=zip" ]] || [[ "$1" == "zip" ]] || [[ -z "$1" ]]; then
  FORMAT="zip"
echo "Selected format: zip (default)"
else
  echo "Invalid format specified: $1. Use 'zip' or 'tar'. Defaulting to 'zip'."
  FORMAT="zip"
fi

# Determine archive extension based on format
if [[ "$FORMAT" == "zip" ]]; then
  ARCHIVE_EXT=".zip"
else
  ARCHIVE_EXT=".tar.gz"
fi

# --- Download and Extract Function ---
download_extract() {
  local base_name=$1
  local target_dir=$2
  local archive_file="${base_name}${ARCHIVE_EXT}"
  local url="${BASE_URL}${archive_file}"
  local temp_archive="${TMP_DIR}/${archive_file}"

  echo "Processing ${archive_file} (Format: ${FORMAT})..."
  echo "  Downloading from ${url}..."

  # Download using curl
  if curl -L -sS -f -o "${temp_archive}" "${url}"; then
    echo "  Extracting to ${target_dir}..."
    
    # Ensure target directory exists
    mkdir -p "${target_dir}"
    
    local extract_success=1 # 0 for success, 1 for failure initially

    # Extract based on format
    if [[ "$FORMAT" == "zip" ]]; then
      unzip -oq "${temp_archive}" -d "${target_dir}"
      extract_success=$?
    else # tar
      tar -xzf "${temp_archive}" -C "${target_dir}"
      extract_success=$?
    fi

    # Check extraction exit status
    if [ ${extract_success} -eq 0 ]; then
      echo "  Successfully extracted ${archive_file}."
    else
      echo "  ERROR: Failed to extract ${archive_file}. Extraction command returned error ${extract_success}."
      rm -f "${temp_archive}" # Clean up downloaded archive even if extraction failed
      return 1 # Indicate failure
    fi
    
    # Remove the temporary archive file on success
    rm -f "${temp_archive}"
  else
    echo "  ERROR: Failed to download ${url}. Curl returned error."
    return 1 # Indicate failure
  fi

  echo "--------------------"
}

# --- Schema Downloads (Pass base name now) ---

# XJDF Schemas
download_extract "jdfschema_2_x" "${SCHEMA_DIR}/xjdf/2.x/" || exit 1
download_extract "jdfschema_2_2" "${SCHEMA_DIR}/xjdf/2.2/" || exit 1
download_extract "jdfschema_2_1" "${SCHEMA_DIR}/xjdf/2.1/" || exit 1
download_extract "jdfschema_2_0" "${SCHEMA_DIR}/xjdf/2.0/" || exit 1

# JDF Schemas
download_extract "jdfschema_1_x" "${SCHEMA_DIR}/jdf/1.x/" || exit 1
download_extract "jdfschema_1_8" "${SCHEMA_DIR}/jdf/1.8/" || exit 1
download_extract "jdfschema_1_7" "${SCHEMA_DIR}/jdf/1.7/" || exit 1
download_extract "jdfschema_1_6" "${SCHEMA_DIR}/jdf/1.6/" || exit 1
download_extract "jdfschema_1_5" "${SCHEMA_DIR}/jdf/1.5/" || exit 1
download_extract "jdfschema_1_4" "${SCHEMA_DIR}/jdf/1.4/" || exit 1
download_extract "jdfschema_1_3" "${SCHEMA_DIR}/jdf/1.3/" || exit 1
download_extract "jdfschema_1_2" "${SCHEMA_DIR}/jdf/1.2/" || exit 1
download_extract "jdfschema_1_1" "${SCHEMA_DIR}/jdf/1.1/" || exit 1

# PrintTalk Schemas
download_extract "ptk_schema_2_x" "${SCHEMA_DIR}/ptk/2.x/" || exit 1
download_extract "ptk_schema_2_2" "${SCHEMA_DIR}/ptk/2.2/" || exit 1
download_extract "ptk_schema_2_1" "${SCHEMA_DIR}/ptk/2.1/" || exit 1
download_extract "ptk_schema_2_0" "${SCHEMA_DIR}/ptk/2.0/" || exit 1
download_extract "ptk_schema_1_5" "${SCHEMA_DIR}/ptk/1.5/" || exit 1
download_extract "ptk_schema_1_4" "${SCHEMA_DIR}/ptk/1.4/" || exit 1
download_extract "ptk_schema_1_3" "${SCHEMA_DIR}/ptk/1.3/" || exit 1
download_extract "ptk_schema_1_2" "${SCHEMA_DIR}/ptk/1.2/" || exit 1
download_extract "ptk_schema_1_1" "${SCHEMA_DIR}/ptk/1.1/" || exit 1

echo "--- Schema Download Complete ---"

exit 0